package com.zone.vehicle.superapp.view.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.zone.lib.download.enums.AppStatusEnum;
import com.zone.lib.download.db.DownloadDaoUtilsStore;
import com.zone.lib.download.db.entity.Download;
import com.zone.lib.download.utils.AppStatusUpdateNotify;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;

import static com.zone.lib.download.DownloadConstant.JD_ACTION_INSTALL;

/**
 * @Author: pkqlqx
 * @CreateDate: 2021/12/20 18:05
 * @Description:
 */
public class SuperAppReceiver extends BroadcastReceiver {
    public static final String TAG = SuperAppReceiver.class.getName();

    private static final String EVENT_DOWNLOADED = "downloaded";
    private static final String EVENT_DELETED = "deleted";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "JD-receive onReceive:"+intent.getAction());
        String action = intent.getAction();
        if(TextUtils.equals(action, JD_ACTION_INSTALL)){
            String event = intent.getStringExtra("event");
            String appId = intent.getStringExtra("appId");
            String appType = intent.getStringExtra("appType");
            Log.d(TAG, "JD-event="+event+" appId="+appId+" appType="+appType);
            if (StringUtils.isNotEmpty(event) && event.equals(EVENT_DOWNLOADED)){
                //小程序下载完成
                Download download = DownloadDaoUtilsStore.getInstance().getDownloadByCode(appId);
                if (ObjectUtils.isNotEmpty(download)){
                    download.setDownloadStatus(AppStatusEnum.INSTALL_SUCCESS.CODE);
                    AppStatusUpdateNotify.appNotify(context, download);
                }
            }else if (StringUtils.isNotEmpty(event) && event.equals(EVENT_DELETED)){
                //小程序删除
                DownloadDaoUtilsStore.getInstance().deleteByCode(appId);
            }
        }

    }
}
